#!/usr/bin/env python
# encoding: utf-8
__author__ = 'Zjay '
"""
@version: 3.6.3
@author: Zjay 
@license: Apache Licence 
@site: 
@software: PyCharm
@file: Spider_cookie.py
@time: 2017/12/13 11:36
"""

import urllib.request as request
import urllib.parse as parse
import http.cookiejar as cookiejar

url = 'http://bbs.chinaunix.net/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=L768q'
postdata = parse.urlencode({'username': 'weisuen', 'password': 'aA123456'}).encode('utf-8')
req = request.Request(url, postdata)
req.add_header("User-Agent",
               "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
cjar = cookiejar.CookieJar()
opener = request.build_opener(request.HTTPCookieProcessor(cjar))
# 思路： 使用 cookiejar.CookieJar() 创建一个全新的cookier,并用其创建一个opener 对象，并 使用,install_opener 使opener 称为一个全局的，提供后面使用
request.install_opener(opener=opener)
file = opener.open(req)
data = file.read()
file = open('', 'wb')
file.write()
